<?php
 /**     
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */

/**
 * classes/AtomParser.php
 */
require_once 'classes/AtomParser.php';
/**
 * HomePageModel Class to process functionality of showing graph on home page
 * @access public
 */
class HomePageModel
{
	/** 
     * Class constructor.
	 * @access public
	 *
	 */
	public function __construct(){}
	/**
	 * This function validate form field data such as From Year and To year
	 * @access public
	 * @param array $postFields Home Page Form Field data
	 * @return array $errors Array of error messages if any
	 */
	public function validateFormFields($postFields = null)
	{
		$errors = array();

		try{
			if(($postFields['listFromYear'] > $postFields['listToYear']) &&  $postFields['rdoGroupFilter'] === 'year'){
				$errors[] = "Start year must be less than end year.";
			}
		} catch (Exception $e){
			throw($e);
		}
		return $errors;
	}
	/**
	 * The function to get region list
	 * @access public
	 *
	 * @return array $regionListArr Array of result and error
	 */
	public function getRegionList()
	{
		global $httpRequestObj;
		
		$regionListArr = array();
		$requestInfoArr = array();

		try{
			$url = REGIONS_SERVICE_URL;
			$httpVerb = 'GET';
			
			//$headerValue = "WRAPv0.8" . " " . urldecode($_SESSION['token']); 
      $token =  "WRAP" . " " . 'access_token="' . urldecode($_SESSION['token']) . '"';
      $headerValue = $token; 
			$head['X-Authorization'] = $headerValue;
			
			
			$requestInfoArr['http_method']  = $httpVerb;
			$requestInfoArr['url']  = $url; 
			if(!empty($head)){
				foreach($head as $key => $value){
					$requestInfoArr[$key]  = urlencode($value);
				}
			}

			//Write Audit Log Details
			LoggerClass::writeLogEntry(REQUEST_TO_SALESDASHBOARD_SERVICE.'-'.GET_REGIONS, $requestInfoArr);
			
			$regionsFeedData = $httpRequestObj->sendRequest($httpVerb, $url ,array() ,$head);			
			
			
			$httpCode = $regionsFeedData['http_code'];
			$response = $regionsFeedData['response'];
			
			//Write Audit Log Details
			LoggerClass::writeLogEntry(RESPONSE_FROM_SALESDASHBOARD_SERVICE.'-'.GET_REGIONS, $response);
			
			if ($httpCode == '202'){
				$regionObj = new AtomParser($response);
				$regionListArr['result'] = $regionObj->parseString();
				
			} else {
				$regionListArr['error'] = 'Error Code '.$httpCode.':'.getStatusCodeMessage($httpCode).' For get regions';
			}
		} catch (Exception $e){
			throw($e);

		}
		return $regionListArr;
	}
	/**
	 * The function to get product list
	 * @access public
	 *
	 * @return array $productsArr Array of result and error
	 */
	public function getProductList()
	{
		global $httpRequestObj;

		$productsArr = array();
		$requestInfoArr = array();

		try{
			$url = PRODUCTS_SERVICE_URL;
			$httpVerb = 'GET';
			 

			//$headerValue = "WRAPv0.8" . " " . urldecode($_SESSION['token']);
      $token =  "WRAP" . " " . 'access_token="' . urldecode($_SESSION['token']) . '"';
      $headerValue = $token; 
			$head['X-Authorization'] = $headerValue;
			
			$requestInfoArr['http_method']  = $httpVerb;
			$requestInfoArr['url']  = $url; 
			if(!empty($head)){
				foreach($head as $key => $value){
					$requestInfoArr[$key]  = urlencode($value);
				}
			}			
			
			//Write Audit Log Details
			LoggerClass::writeLogEntry(REQUEST_TO_SALESDASHBOARD_SERVICE.'-'.GET_PRODUCTS, $requestInfoArr);
			
			$productsFeedData = $httpRequestObj->sendRequest($httpVerb, $url,array(), $head);		
			

			$httpCode = $productsFeedData['http_code'];
			$response = $productsFeedData['response'];
			
			//Write Audit Log Details
			LoggerClass::writeLogEntry(RESPONSE_FROM_SALESDASHBOARD_SERVICE.'-'.GET_PRODUCTS,$response);
			
			if ($httpCode == '202'){
				$productObj = new AtomParser($response);
				$productsArr['result'] = $productObj->parseString();

			} else {
				$productsArr['error'] = 'Error Code '.$httpCode.':'.getStatusCodeMessage($httpCode).' for get products';
			}

		} catch (Exception $e){
			throw($e);

		}
		return $productsArr;
	}
	/**
	 * The function to consolidated sales order data
	 * @access public
	 *
     * @param integer $productId Product Id
 	 * @param integer $regionId Region Id
	 * @param integer $year Year
	 * @param integer $fromYear From Year
	 * @param integer $toYear To Year
     * @param integer $isYearlyOrMonthly  Monthly/Yearly option
	 * @return array $salesDataArr Array of result and error
	 */
	public function getConsolidatedSalesData($productId="", $regionId ="",$year = "2009", $fromYear="", $toYear="", $isYearlyOrMonthly="monthly")
	{
		global $httpRequestObj;

		$salesDataArr = array();
		$requestInfoArr = array();
		
		$qryString = "";

		try {
			//Prepare URL Query String
			if (isset($productId) && $productId > 0){
				$qryString .= "/productid/".$productId."/";
			}
			if (isset($regionId) && $regionId > 0){
				$qryString .= "/regionid/".$regionId."/";
			}
			if($isYearlyOrMonthly == "monthly"){
				if (isset($year) && $year > 0){
					$qryString .= "/year/".$year."/";
				}
			}
			if($isYearlyOrMonthly == "yearly"){
				if (isset($fromYear) && $fromYear > 0){
					$qryString .= "/fromyear/".$fromYear."/";
				}
				if (isset($toYear) && $toYear > 0){
					$qryString .= "/toyear/".$toYear."/";
				}
			}
			$httpVerb = 'GET';
			$url = SALES_DATA_SERVICE_URL.$qryString;
			
			//$headerValue = "WRAPv0.8" . " " . urldecode($_SESSION['token']); 
      $token =  "WRAP" . " " . 'access_token="' . urldecode($_SESSION['token']) . '"';
      $headerValue = $token; 
			$head['X-Authorization'] = $headerValue;

			$requestInfoArr['http_method']  = $httpVerb;
			$requestInfoArr['url']  = $url; 
			if(!empty($head)){
				foreach($head as $key => $value){
					$requestInfoArr[$key]  = urlencode($value);
				}
			}

			//Write Audit Log Details
			LoggerClass::writeLogEntry(REQUEST_TO_SALESDASHBOARD_SERVICE.'-'.GET_SALESDATA, $requestInfoArr);
			
			$salesFeedData = $httpRequestObj->sendRequest($httpVerb, $url, array(), $head);
			
			$httpCode = $salesFeedData['http_code'];
			$response = $salesFeedData['response'];
			
			//Write Audit Log Details
			LoggerClass::writeLogEntry(RESPONSE_FROM_SALESDASHBOARD_SERVICE.'-'.GET_SALESDATA, $response);
			
			//Parse response
			if ($httpCode == '202'){
				$salesObj = new AtomParser($response);
				$salesDataArr['result'] = $salesObj->parseString();
			} else {
				$salesDataArr['error'] = $httpCode;
			}
			

		} catch (Exception $e){
			throw($e);

		}
		return $salesDataArr;
	}
}
?>